import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from '/src/App.vue'
import router from '/src/components/router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import axios from "axios";
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

import { createPinia } from 'pinia'


import piniaPersistedState from "pinia-plugin-persistedstate"
const pinia = createPinia()
pinia.use(piniaPersistedState);

const app = createApp(App)

app.use(ElementPlus, {
    locale: zhCn,
})
// 是否在启动时生成生产提示
app.config.productionTip = false
//将axios进行全局设置
app.config.globalProperties.$axios = axios
// 指定 axios 的全局配置，指定为后端服务器地址
axios.defaults.baseURL = 'http://localhost:8081/'
//指定axios的请求头中携带cookie
axios.defaults.withCredentials = true;


// 请求拦截器
axios.interceptors.request.use(config => {
    // 登录接口不携带 token
    if (!config.url.startsWith('/user')) {
        const token = localStorage.getItem('token')
        if (token) {
            // 请求体  body   Header 请求头（放置数据{一般不是自定义  token}）
            config.headers['authorization'] = `Bearer ${token}`
        }
    }
    return config
}, error => {
    return Promise.reject(error)
})

// 全局拦截器设置
// axios.interceptors.response.use(
//     response => response,
//     error => {
//         if (error.response && error.response.status === 401) {
//             alert("登录失效，请重新登录！");
//             window.location.href = "http://localhost:9000/";
//         } else {
//             alert(error.response?.data || "请求失败");
//         }
//         return Promise.reject(error);
//     }
// );

//定义图片地址全局变量，所有组件都可以使用

app.use(pinia)
app.use(router)
app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.mount('#app')


